package com.llpy.Service;


import com.llpy.Utils.JwtTokenUtil;
import com.llpy.dto.UserDto;
import com.llpy.mapper.UserMapper;
import com.llpy.pojo.User;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class UserService {
    private final UserMapper userMapper;

    @Autowired
    private ImgService imgService;

    @Autowired
    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    //注册用户
    public User createUser(User user) {
        List<User> userList = userMapper.findAll();
        //遍历所有用户，如果用户名存在，返回null
        for (User user1 : userList) {
            if(user1.getUsername().equals(user.getUsername())){
                return null;
            }
        }
//        如果不存在，存入一条新的用户信息
        System.out.println(user);
        user.setId(null);
        user.setCreateDate(new Date());
        return userMapper.save(user);
    }



    //验证登录信息
    public ResponseEntity<?> getByName(User user) {
        //登录后得到当前登录信息
        User byName = userMapper.findByUsername(user.getUsername());

        if (byName != null && byName.getPassword().equals(user.getPassword())) {
            String jwt = JwtTokenUtil.createToken(byName.getUsername(),"admin");
            System.out.println(jwt);
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(byName,userDto);
            userDto.setJWT(jwt);
            // 用户存在且密码正确，返回成功的响应
            userDto.setPassword(null);

            return ResponseEntity.ok(userDto);
        } else {
            // 用户不存在或密码错误，返回失败的响应
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
    }

    //更改用户头像
    public User updateImg(String id,String userImg){
//        根据传入的id和图片id更改用户头像
        Optional<User> byId = userMapper.findById(id);  //查找用户存不存在
        if (byId.isPresent()) {
            User u = byId.get();
            //判断是否为默认头像，如果是，则不添加新的
            if(!u.getUserImg().equals("646232b8886b0000d3003352")){  //646232b8886b0000d3003352默认头像id
                imgService.deleteImg(new ObjectId(u.getUserImg())); //会根据图片id删除图片
            }
            //如果不是，删除旧头像，存入新的头像
            u.setUserImg(userImg);  //将用户的图片id改为新的
            userMapper.delete(u); //删除用户
            return userMapper.save(u);  //存入新的用户
        }
        return null;
    }

    //更改用户信息
    public User updateInfo(User u,String id){
        //根据新传入用户信息，更新用户信息
        Optional<User> byId = userMapper.findById(id);
        if (byId.isPresent()) {
            User user = byId.get();
            user.setUserImg(u.getUserImg());
            user.setNickname(u.getNickname());
            user.setGender(u.getGender());
            user.setBirthday(u.getBirthday());
            return userMapper.save(user);  //返回新的用户数据
        }
        return null;
    }

    public boolean deleteUser(String id1) {
        userMapper.deleteById(id1);
        return true;
    }
}

